/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.controller;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import model.controller.exceptions.NonexistentEntityException;
import model.controller.exceptions.PreexistingEntityException;
import model.db.Projects;
import model.db.ProjectsHistory;

/**
 *
 * @author mwiatrowski
 */
public class ProjectsHistoryJpaController implements Serializable {

    public ProjectsHistoryJpaController(EntityManagerFactory emf) {
        this.emf = emf;
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(ProjectsHistory projectsHistory) throws PreexistingEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Projects idProjects = projectsHistory.getIdProjects();
            if (idProjects != null) {
                idProjects = em.getReference(idProjects.getClass(), idProjects.getIdProjects());
                projectsHistory.setIdProjects(idProjects);
            }
            em.persist(projectsHistory);
            if (idProjects != null) {
                idProjects.getProjectsHistoryCollection().add(projectsHistory);
                idProjects = em.merge(idProjects);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findProjectsHistory(projectsHistory.getId()) != null) {
                throw new PreexistingEntityException("ProjectsHistory " + projectsHistory + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(ProjectsHistory projectsHistory) throws NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ProjectsHistory persistentProjectsHistory = em.find(ProjectsHistory.class, projectsHistory.getId());
            Projects idProjectsOld = persistentProjectsHistory.getIdProjects();
            Projects idProjectsNew = projectsHistory.getIdProjects();
            if (idProjectsNew != null) {
                idProjectsNew = em.getReference(idProjectsNew.getClass(), idProjectsNew.getIdProjects());
                projectsHistory.setIdProjects(idProjectsNew);
            }
            projectsHistory = em.merge(projectsHistory);
            if (idProjectsOld != null && !idProjectsOld.equals(idProjectsNew)) {
                idProjectsOld.getProjectsHistoryCollection().remove(projectsHistory);
                idProjectsOld = em.merge(idProjectsOld);
            }
            if (idProjectsNew != null && !idProjectsNew.equals(idProjectsOld)) {
                idProjectsNew.getProjectsHistoryCollection().add(projectsHistory);
                idProjectsNew = em.merge(idProjectsNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                BigDecimal id = projectsHistory.getId();
                if (findProjectsHistory(id) == null) {
                    throw new NonexistentEntityException("The projectsHistory with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(BigDecimal id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            ProjectsHistory projectsHistory;
            try {
                projectsHistory = em.getReference(ProjectsHistory.class, id);
                projectsHistory.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The projectsHistory with id " + id + " no longer exists.", enfe);
            }
            Projects idProjects = projectsHistory.getIdProjects();
            if (idProjects != null) {
                idProjects.getProjectsHistoryCollection().remove(projectsHistory);
                idProjects = em.merge(idProjects);
            }
            em.remove(projectsHistory);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<ProjectsHistory> findProjectsHistoryEntities() {
        return findProjectsHistoryEntities(true, -1, -1);
    }

    public List<ProjectsHistory> findProjectsHistoryEntities(int maxResults, int firstResult) {
        return findProjectsHistoryEntities(false, maxResults, firstResult);
    }

    private List<ProjectsHistory> findProjectsHistoryEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(ProjectsHistory.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public ProjectsHistory findProjectsHistory(BigDecimal id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(ProjectsHistory.class, id);
        } finally {
            em.close();
        }
    }

    public int getProjectsHistoryCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<ProjectsHistory> rt = cq.from(ProjectsHistory.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }
    
    
    
}
